package soulkun.library.helper.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import soulkun.library.utils.toPx

/**
 * LinearLayoutManager布局和GridLayoutManager布局的间隔通用实现类
 * 其中填入的数字已自动转成Px，无需事先转化
 * 只考虑每个Item占一格的情况
 * GridLayoutManager：竖直布局——第一行 top:space bottom:space 其他 bottom:space
 *                    水平布局——第一列 left:space right:space 其他 right:space
 */
class GridSimpleSpaceItemDecorationHelper(
    private val marginTopDp: Int,
    private val marginBottomDp: Int,
    private val marginLeftDp: Int,
    private val marginRightDp: Int,
    private val verticalSpaceDp: Int,
    private val horizontalSpaceDp: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            val adapterPosition = parent.getChildAdapterPosition(view)
            val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams
            val spanCount = layoutManager.spanCount
            //竖直方向
            if (layoutManager.orientation == GridLayoutManager.VERTICAL) {
                when{
                    layoutManager.itemCount == 0 -> {
                    }
                    layoutManager.itemCount <= spanCount -> {
                        when (layoutParams.spanIndex) {
                            0 -> {
                                outRect.left = marginLeftDp.toPx()
                                outRect.right = horizontalSpaceDp.toPx()
                            }
                            layoutManager.itemCount - 1 -> {
                                outRect.right = marginRightDp.toPx()
                            }
                            else -> {
                                outRect.right = horizontalSpaceDp.toPx()
                            }
                        }
                        outRect.top = marginTopDp.toPx()
                        outRect.bottom = marginBottomDp.toPx()
                    }
                    else -> {
                        when {
                            layoutManager.spanSizeLookup.getSpanGroupIndex(
                                adapterPosition,
                                spanCount
                            ) == 0 -> {
                                outRect.top = marginTopDp.toPx()
                                outRect.bottom = verticalSpaceDp.toPx()
                            }
                            layoutManager.spanSizeLookup.getSpanGroupIndex(
                                adapterPosition,
                                spanCount
                            ) == layoutManager.itemCount / spanCount -> {
                                outRect.bottom = marginBottomDp.toPx()
                            }
                            else -> {
                                outRect.bottom = verticalSpaceDp.toPx()
                            }
                        }
                        when (layoutParams.spanIndex) {
                            0 -> {
                                outRect.left = marginLeftDp.toPx()
                                outRect.right = horizontalSpaceDp.toPx()
                            }
                            spanCount - 1 -> {
                                outRect.right = marginRightDp.toPx()
                            }
                            else -> {
                                outRect.right = horizontalSpaceDp.toPx()
                            }
                        }
                    }
                }
            }
            //水平方向
            else {
                when{
                    layoutManager.itemCount == 0 -> {
                    }
                    layoutManager.itemCount <= spanCount -> {
                        when (layoutParams.spanIndex) {
                            0 -> {
                                outRect.top = marginTopDp.toPx()
                                outRect.bottom = verticalSpaceDp.toPx()
                            }
                            layoutManager.itemCount - 1 -> {
                                outRect.bottom = marginBottomDp.toPx()
                            }
                            else -> {
                                outRect.bottom = verticalSpaceDp.toPx()
                            }
                        }
                        outRect.left = marginLeftDp.toPx()
                        outRect.right = marginRightDp.toPx()
                    }
                    else -> {
                        when {
                            layoutManager.spanSizeLookup.getSpanGroupIndex(
                                adapterPosition,
                                spanCount
                            ) == 0 -> {
                                outRect.left = marginLeftDp.toPx()
                                outRect.right = horizontalSpaceDp.toPx()
                            }
                            layoutManager.spanSizeLookup.getSpanGroupIndex(
                                adapterPosition,
                                spanCount
                            ) == layoutManager.itemCount / spanCount -> {
                                outRect.right = marginRightDp.toPx()
                            }
                            else -> {
                                outRect.right = horizontalSpaceDp.toPx()
                            }
                        }
                        when (layoutParams.spanIndex) {
                            0 -> {
                                outRect.top = marginTopDp.toPx()
                                outRect.bottom = verticalSpaceDp.toPx()
                            }
                            spanCount - 1 -> {
                                outRect.bottom = marginBottomDp.toPx()
                            }
                            else -> {
                                outRect.bottom = verticalSpaceDp.toPx()
                            }
                        }
                    }
                }
            }
        }
    }
}